import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;


import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-03-22
 * Time:21:02
 */

public class TestJDBC {
    public static void main(String[] args) throws SQLException {
        //1.创建好数据源
        DataSource dataSource = new MysqlDataSource();
        //设置数据库所在的地址
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/practice?characterEncoding=utf8&useSSL=false");
        //设置登录数据库的用户名
        ((MysqlDataSource)dataSource).setUser("root");
        //设置登录数据库的密码
        ((MysqlDataSource)dataSource).setPassword("0000");

        //2.建立连接
        Connection connection = dataSource.getConnection();

        //3.拼装SQL语句
        String sql = "insert into student values (?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,6);
        statement.setString(2,"ok");
        System.out.println("statement:"+statement);

        //4.执行SQL
        int ret = statement.executeUpdate();
        System.out.println(ret);

        //5.回收资源
        statement.close();
        connection.close();
    }
}
